<?php
			session_start();
?>

<!DOCTYPE html>

<html>
	<head>
		<meta charset="utf-8" />
		<title>Page de Connexion</title>
	</head>
	
	<body>
		<?php
			if(isset($_SESSION["connected"]) && $_SESSION["connected"] == true)
				{
					

				}
			else 
				{
					if(isset($_POST["login"]) == true)
						{
							$mysqli = new mysqli("localhost", "root", "root", "forum");

							/* Vérification de la connexion */
							if (mysqli_connect_errno())
								{
    								echo 'Échec de la connexion : ' . mysqli_error();
    								exit;
								}

							$requete = 'SELECT * FROM user WHERE login=\'' . $_POST["login"] . '\' AND mdp=\'' . sha1($_POST["mdp"]) . '\';';

							/* Retourne le nom de la base de données courante */
							if ($result = $mysqli->query($requete))
								{
    								$array = $result->fetch_array();
    								if ($array["login"] == $_POST["login"] && $array["mdp"] == sha1($_POST["mdp"]))
    									{
    										$_SESSION["connected"] = true;
    										$_SESSION["login"] = $array["login"];
                                            $_SESSION["idUser"] = $array["id"];
    										if ($array["isAdmin"] == true)
	    										{
	    											$_SESSION["isAdmin"] = true;
												}
											else
												{
													$_SESSION['isAdmin'] = false;
												}
											if ($array["isModo"] == true)
	    										{
	    											$_SESSION["isModo"] = true;
												}
											else
												{
													$_SESSION['isModo'] = false;
												}
    									}
    								$result->close();
								}
							else 
								{
									echo '<p>La requ&ecirc;te a echouée</p>';
								}

							$mysqli->close();
						}
					else 
						{
							?>
						
								<script type="text/JavaScript">
									document.location.href="index.php";
								</script>
							
							<?php
						}
						?>
						
						<script type="text/JavaScript">
							document.location.href="index.php";
						</script>
						
						<?php
				}

		?>
		<script type="text/JavaScript">
							document.location.href="index.php";
						</script>
	</body>
</html>